//
//  HamatonCarSettingSelectViewController.swift
//  hamaton-app
//
//  Created by intyt on 2024/4/11.
//

import UIKit
import SnapKit

typealias HamatonCarSettingSelectViewSelect = (HamatonCarModel) -> Void

class HamatonCarSettingSelectViewController: HamatonBaseViewController {
    // MARK: 返回事件闭包回调
    var backAction: HamatonCarSettingSelectViewSelect?
    var type: Int = 0
    var listArr :[HamatonCarModel] = []

    lazy var tableView: UITableView = {
        let tv = UITableView.init(frame: CGRect.zero, style: .plain)
        tv.backgroundColor = .white
        tv.separatorStyle = .none
        tv.delegate = self
        tv.dataSource = self
        tv.register(HamatonCarSettingSelectTableViewCell.self, forCellReuseIdentifier: "HamatonCarSettingSelectTableViewCell")
        view.addSubview(tv)
        return tv
    }()
    // 循环插入列表数据
    func getTableList(dictionary: [String: String]) {
//        listArr.removeAll()
//        for (value, label) in dictionary {
//            let gModel: HamatonCarModel = HamatonCarModel(id: value, name: label)
//            listArr.append(gModel)
//        }
        listArr.removeAll()
        for key in dictionary.keys.sorted(by: { Int($0)! < Int($1)! }) {
            if let value = dictionary[key] {
                    print("\(key): \(value)")
                let gModel: HamatonCarModel = HamatonCarModel(id: key, name: value)
                listArr.append(gModel)
            }
            
        }
    }
    
    func selectTypeEnumFun(selectTypeEnum: SelectTypeEnum) {
        switch selectTypeEnum {
        case .a:
            navigationItem.title = "选择车辆类型"
            self.getTableList(dictionary: CarTypeOption)
        case .b:
            navigationItem.title = "选择通讯类型"
            self.getTableList(dictionary: CommunicationTypeList)
        case .c:
            navigationItem.title = "选择CAN通讯波特率"
            self.getTableList(dictionary: CANCommunicationBaudRateOption)
        case .d:
            navigationItem.title = "选择CAN通讯地址"
            var dictionary: [String: String] = [:]
            for i in 1...254 {
                let key = String(i)
                let value = String(i)
                dictionary[key] = value
            }
            self.getTableList(dictionary: dictionary)
        case .e:
            navigationItem.title = "选择RF通道1"
            self.getTableList(dictionary: PCTRFChannel1Option)
        case .f:
            navigationItem.title = "选择RF通道2"
            self.getTableList(dictionary: PCTRFChannel2Option)
        case .g:
            navigationItem.title = "选择挂车数量"
            var dictionary: [String: String] = [:]
            for i in 0...6 {
                let key = String(i)
                let value = String(i)
                dictionary[key] = value
            }
            self.getTableList(dictionary: dictionary)
        case .h:
            navigationItem.title = "选择传感器类型"
            self.getTableList(dictionary: CgqTypeOption1)
        case .i:
            navigationItem.title = "选择压力单位"
            self.getTableList(dictionary: YalidanweiOption)
        case .j:
            navigationItem.title = "选择温度单位"
            self.getTableList(dictionary: WendudanweiOption)
        }
        
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        navigationItem.title = "选择"
        
        tableView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }
    }

}

extension HamatonCarSettingSelectViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "HamatonCarSettingSelectTableViewCell", for: indexPath) as! HamatonCarSettingSelectTableViewCell
        if indexPath.row < listArr.count {
            cell.listModel = listArr[indexPath.row]
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row < listArr.count {
            if backAction != nil {
                backAction!(listArr[indexPath.row])
            }
            self.navigationController?.popViewController(animated: false)
        }
    }
    
    
}
